package b2.widget.fort
{
	import b2.config.*;
	import b2.net.*;
	import b2.player.*;
	import b2.scene.guide.*;
	import b2.util.*;
	import b2.widget.*;
	import b2.widget.tab.*;
	
	import com.greensock.TweenLite;
	
	import flash.display.Sprite;
	import flash.events.MouseEvent;
	import flash.filters.*;
	import flash.text.*;

	public class FortressDialog extends Sprite
	{
		public var currentSelectedCampItem:CampItem;
		public var fortIndex:int;
		private var backGround:Sprite;
		//右侧战役详情
		public var campInfoPane:CampInfoPane;
		//帮助容器
		private var guideContainter:Sprite;
		//帮助步骤
		private var guideStep:int;
		
		public function FortressDialog(fortIndex:int, campIndexToSelect:int = -1)
		{
			super();
			this.fortIndex = fortIndex;
			var fortressConfig:FortressConfig = Config.fortressConfigs[fortIndex];
			//背景图片
			var FortressDialogBgr:Class = ResourceHelper.getInstance().getClassByName("FortressDialogBgr");
			var fortressDialogBgr:Sprite = new FortressDialogBgr();
			addChild(fortressDialogBgr);
			backGround = new Sprite();
			addChild(backGround);
			//标题
			var titleText:TextField = new TextField();
			titleText.selectable = false;
        	titleText.autoSize = TextFieldAutoSize.CENTER;
        	var titleFomat:TextFormat = new TextFormat("微软雅黑", 16, 0xFFFFFF);
        	titleFomat.letterSpacing = 10;
			titleText.defaultTextFormat = titleFomat;
			titleText.filters = [new GlowFilter(0x000000, 1, 1.3, 1.3, 100, BitmapFilterQuality.HIGH)];
			titleText.htmlText = fortressConfig.name;
			titleText.x = (this.width - titleText.width) / 2;
			titleText.y = 15;
			addChild(titleText);
			//左侧战役列表
			var campText:TextField = new TextField();
			campText.selectable = false;
        	campText.autoSize = TextFieldAutoSize.LEFT;
			campText.defaultTextFormat = new TextFormat("微软雅黑", 15, 0xFFFFFF);
			campText.filters = [new GlowFilter(0x000000, 1, 1.3, 1.3, 100, BitmapFilterQuality.HIGH)];
			campText.text = "战役列表";
			campText.x = 65;
			campText.y = 72;
			addChild(campText);
			var campListPane:Tab = new Tab("", 1, 6);
			campListPane.setPagePos(20, 120);
			campListPane.setPageSelectorPos(50, 495);
			addChild(campListPane);
			var me:PlayerInfo = PlayerInfoManager.getInstance().players[PlayerInfoManager.getInstance().myUserId];
			var campList:Array = new Array();
			//新手引导
			if (GuideScene.getInstance().enabled)
			{
				campList.push(0);
			}
			else
			{
				for (var i:int = 0; i < fortressConfig.campIndex.length; i++)
				{
					var campIndex:int = parseInt(fortressConfig.campIndex[i]);
					var campConfig:CampConfig = Config.campConfigs[campIndex];
					//剔除新手引导战役和调试战役
					if (campConfig.requireLevel <= me.level && campIndex != 0 && !(Profile.DEBUG == 0 && campIndex == 1) && 
						(campConfig.requireTaskIndex == 0 || me.getTaskStatus(campConfig.requireTaskIndex) == 2))
					{
						campList.push(campIndex);
					}
				}
				campList.sort(sortCamp);
			}
			var targetPage:int = 0;
			for (i = 0; i < campList.length; i++)
			{
				var campItem:CampItem = new CampItem(campList[i]);
				campItem.addEventListener(MouseEvent.CLICK, onCampItemClick);
				campListPane.addItem(campItem, 169, 53);
				if (campIndexToSelect < 0 && i == 0 || campIndexToSelect == campList[i])
				{
					campItem.selected = true;
					currentSelectedCampItem = campItem;
					targetPage = i / 6;
				}
			}
			campListPane.currentPageIndex = targetPage;
			//右侧战役详情
			campInfoPane = new CampInfoPane(this);
			campInfoPane.x = 220;
			campInfoPane.y = 56;
			addChild(campInfoPane);
//			var CampInfoTitle:Class = ResourceHelper.getInstance().getClassByName("ButtonBgr6");
//			var HighlightTitleBgr:Class = ResourceHelper.getInstance().getClassByName("ButtonBgr4");
//			var campDailyRecordTab:CampRecordTab = new CampRecordTab("每日纪录");
//			campInfoPane.addTab(CampInfoTitle, campDailyRecordTab.tabName, campDailyRecordTab, HighlightTitleBgr);
//			var campRecordTab:CampRecordTab = new CampRecordTab("全服纪录");
//			campInfoPane.addTab(CampInfoTitle, campRecordTab.tabName, campRecordTab, HighlightTitleBgr);
			//关闭按钮
			var CloseButton:Class = ResourceHelper.getInstance().getClassByName("CloseButton");
			var closeButton:BasicButton = new BasicButton(CloseButton);
			closeButton.x = this.width - 44;
			closeButton.y = 16;
			addChild(closeButton);
			closeButton.addEventListener(MouseEvent.CLICK, onCloseButtonClick);
			//加入房间界面
			var joinRoomDialog:JoinRoomDialog = new JoinRoomDialog();
			joinRoomDialog.x = this.width;
			joinRoomDialog.y = 220;
			addChild(joinRoomDialog);
			this.guideStep = 4;
		}
		
		public function close():void
		{
			if (this.parent != null)
			{
				this.parent.removeChild(this);
			}
		}
		
		public function startGuide():void
		{
			if (guideStep >= 4)
			{
				guideStep = 0;
				nextGuideStep();
			}
			else
			{
				guideStep = 0;
			}
		}
		
		private function nextGuideStep():void
		{
			if (guideContainter != null && guideContainter.parent != null)
				guideContainter.parent.removeChild(guideContainter);
			guideContainter = new Sprite();
			addChild(guideContainter);
			var blinkRect:BlinkRec;
			var bubbleMessage:BubbleMessage;
			if (guideStep == 0)
			{
				blinkRect = new BlinkRec(20, 112, 185, 360);
				bubbleMessage = new BubbleMessage("单击选择战役", true);
				bubbleMessage.x = 170;
				bubbleMessage.y = 63;
			}
			else if (guideStep == 1)
			{
				blinkRect = new BlinkRec(230, 142, 135, 80);
				bubbleMessage = new BubbleMessage("战役具体信息");
				bubbleMessage.x = 202;
				bubbleMessage.y = 91;
				var blinkRect2:BlinkRec = new BlinkRec(393, 142, 174, 80);
				var bubbleMessage2:BubbleMessage = new BubbleMessage("几率掉落物品", true);
				bubbleMessage2.x = 440;
				bubbleMessage2.y = 91;
				guideContainter.addChild(blinkRect2);
				guideContainter.addChild(bubbleMessage2);
			}
			else if (guideStep == 2)
			{
				blinkRect = new BlinkRec(230, 254, 334, 76);
				bubbleMessage = new BubbleMessage("敌方坦克信息", true);
				bubbleMessage.x = 300;
				bubbleMessage.y = 205;
			}
			else if (guideStep == 3)
			{
				blinkRect = new BlinkRec(230, 364, 334, 114);
				bubbleMessage = new BubbleMessage("可以加入的房间信息", true);
				bubbleMessage.x = 340;
				bubbleMessage.y = 292;
			}
			else
			{
				blinkRect = new BlinkRec(234, 476, 80, 35);
				bubbleMessage = new BubbleMessage("快速开始游戏！");
				bubbleMessage.x = 150;
				bubbleMessage.y = 426;
				blinkRect2 = new BlinkRec(320, 476, 80, 35);
				bubbleMessage2 = new BubbleMessage("创建房间，联机游戏", true);
				bubbleMessage2.x = 390;
				bubbleMessage2.y = 405;
				guideContainter.addChild(blinkRect2);
				guideContainter.addChild(bubbleMessage2);
			}
			guideContainter.addChild(blinkRect);
			guideContainter.addChild(bubbleMessage);
			if (guideStep++ < 4)
			{
				TweenLite.delayedCall(5, nextGuideStep);
			}
		}
		
		private function sortCamp(a:int, b:int):int
		{
			var campConfigA:CampConfig = Config.campConfigs[a];
			var campConfigB:CampConfig = Config.campConfigs[b];
			if (campConfigA.type < campConfigB.type)
				return 1;
			else if (campConfigA.type > campConfigB.type)
				return -1;
			else if (campConfigA.difficulty < campConfigB.difficulty)
				return 1;
			else if (campConfigA.difficulty > campConfigB.difficulty)
				return -1;
			else if (campConfigA.index < campConfigB.index)
				return 1;
			else if (campConfigA.index > campConfigB.index)
				return -1;
			else
				return 0;
		}
		
		private function onCloseButtonClick(event:MouseEvent):void
		{
			close();
		}
		
		private function onCampItemClick(event:MouseEvent):void
		{
			var campItem:CampItem = event.currentTarget as CampItem;
			currentSelectedCampItem.selected = false;
			campItem.selected = true;
			currentSelectedCampItem = campItem;
			campInfoPane.refresh();
		}
		
		public function changeBgr(bgr:Sprite):void
		{
			if (backGround.numChildren > 0)
			{
				backGround.removeChildAt(0);
			}
			backGround.addChild(bgr);
		}
	}
}